package utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;


/**
 * @author lucine
 * @version 1.0 2021/6/18 06:47
 */
public class RabbitMQUtils {

    private static  ConnectionFactory connectionFactory ;

    static{
        connectionFactory =  new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("ems");
        connectionFactory.setVirtualHost("/ems");
    }


    /**
     * 提供连接对象方法
     */
    public static Connection getConnection() throws Exception{
        try{
            return connectionFactory.newConnection();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 提供关闭连接方法
     */
    public static void cloneConnectionAndChannel(Channel channel, Connection connection) throws Exception {
        try{
            if(channel !=null){
                channel.close();
            }
            if(connection !=null){
                connection.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
